<template>
  <div class="app-layout">
    <Navi />
    <div class="main-wrapper">
      <UserInfoBar />
      <div class="main-content">
        <!-- 会根据当前 URL 自动渲染对应的页面组件 -->
        <router-view /> 
      </div>      
      
      <!-- 浮动AI问答组件，在任何页面都可见 -->
      <AIChatFloat />
      
      <footer class="app-footer">
        <div class="footer-content">
          <div class="footer-logo">
            <span class="footer-icon">📊</span>
            <span class="footer-brand">金融工具平台</span>
          </div>
          <p class="footer-copyright">&copy; 2025 提供专业金融分析服务</p>
          <div class="footer-links">
            <a href="#" class="footer-link">关于我们</a>
            <a href="#" class="footer-link">使用条款</a>
            <a href="#" class="footer-link">隐私政策</a>
          </div>
        </div>
      </footer>
    </div>
  </div>
</template>

<script setup>
import { onMounted, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import Navi from './views/Navi.vue'
import UserInfoBar from './views/UserInfoBar.vue'
import AIChatFloat from './components/AIChatFloat.vue'

const router = useRouter()
const route = useRoute()

function isLogin() {
  return !!localStorage.getItem('user')
}

function checkLogin() {
  // 允许访问登录、注册页
  if (['/login', '/register'].includes(route.path)) return
  if (!isLogin()) {
    router.replace('/login')
  }
}

onMounted(checkLogin)
watch(() => route.path, checkLogin)
</script>

<style>
body {
  margin: 0;
  padding: 0;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background-color: var(--color-background);
}

.app-layout {
  display: flex;
  min-height: 100vh;
}

.main-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
  position: relative;
  background-color: #f9fafb;
}

.main-content {
  flex: 1;
  padding: 1.5rem;
  background: linear-gradient(to bottom right, rgba(94, 234, 212, 0.05), rgba(110, 168, 254, 0.05));
  min-height: calc(100vh - 120px);
}

.app-footer {
  padding: 1.5rem;
  background-color: white;
  color: var(--neutral-600);
  font-size: 0.875rem;
  border-top: 1px solid var(--secondary-light);
  margin-top: auto;
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.03);
}

.footer-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  max-width: 1280px;
  margin: 0 auto;
}

.footer-logo {
  display: flex;
  align-items: center;
}

.footer-icon {
  font-size: 1.25rem;
  margin-right: 0.5rem;
}

.footer-brand {
  font-weight: 600;
  color: var(--secondary-dark);
}

.footer-copyright {
  color: var(--neutral-500);
}

.footer-links {
  display: flex;
  gap: 1.5rem;
}

.footer-link {
  color: var(--secondary-dark);
  text-decoration: none;
  transition: color 0.2s;
}

.footer-link:hover {
  color: var(--primary-color);
  text-decoration: underline;
}

@media (max-width: 768px) {
  .app-layout {
    flex-direction: column;
  }
  
  .main-content {
    padding: 1rem;
  }
  
  .footer-content {
    flex-direction: column;
    gap: 1rem;
    text-align: center;
  }
  
  .footer-links {
    justify-content: center;
    gap: 1rem;
  }
}
</style>